package com.android.settings.ios.sound;


import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.settings.ios.OsListFragment;
import com.android.settings.ios.R;
import com.iphone.theme.GroupListAdapter;
import com.iphone.theme.SlipButton;

import java.util.ArrayList;
import java.util.List;

public class SoundFragment extends OsListFragment {

//    private static final String TAG = "SoundFragment";
//    private GroupListAdapter adapter = null;
//    //private List<String> list = new ArrayList<String>();
//    //private List<String> listTag = new ArrayList<String>();
//    private List<GroupListAdapter.Item> items;
//    private Button leftSoftKey = null;
//    private Button rightSoftkey = null;
//    private TextView title;
//    //zhaoni 2014.10.29
//    private ListView mSettingListView;
//    private static int mCurrentPosition = 0;
//    private IntentFilter mIntentFilter;
//    private static final int UPDATE_ADAPTER = 0;
//    //end
//    private AudioManager mAudioManager;
//    private ContentResolver mContentResolver;
//    private int mAudioMode = -1;
//
//    public Ringtone mRingtone = null;
//    public Uri defaultUri = null;
//    public int mVolumeCurrent = -1;
//    public int mVolumeMax = -1;
//
//    private Handler mHandler = new Handler(Looper.myLooper()) {
//
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case UPDATE_ADAPTER:
//                    updateAdapter();
//                    break;
//            }
//        }
//    };
//
//    public void updateAdapter() {
//        mAudioMode = mAudioManager.getRingerMode();
//        defaultUri = RingtoneManager.getActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE);
//        mVolumeCurrent = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
//        mVolumeMax = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
//        if (defaultUri != null) {
//            mRingtone = RingtoneManager.getRingtone(getContext(), defaultUri);
//        }
//        items = createListData();
//        adapter = new GroupListAdapter(getContext(), items);
//        mSettingListView.setAdapter(adapter);
//        mSettingListView.setSelection(mCurrentPosition);
//    }
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
//        mContentResolver = getContext().getContentResolver();
//    }
//
//    @Override
//    protected void onViewInflated(View view, ListView listView) {
//        mSettingListView = listView;
//        mCurrentPosition = 0;
//    }
//
//    @Override
//    public void onResume() {
//        super.onResume();
//        mHandler.sendEmptyMessage(UPDATE_ADAPTER);
//
//        mIntentFilter = new IntentFilter();
//        mIntentFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
//        getContext().registerReceiver(mUpdateListReceiver, mIntentFilter);
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        if (mRingtone != null) {
//            mRingtone.stop();
//        }
//
//        mCurrentPosition = mSettingListView.getFirstVisiblePosition();
//        getContext().unregisterReceiver(mUpdateListReceiver);
//    }
//
//    private final BroadcastReceiver mUpdateListReceiver = new BroadcastReceiver() {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            String action = intent.getAction();
//            if (mRingtone != null) {
//                mRingtone.stop();
//            }
//
//            if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
//                mAudioMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1);
//                if (mAudioMode == AudioManager.RINGER_MODE_VIBRATE) {
//                    Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_IN_SILENT, 1);
//                } else {
//                    Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_IN_SILENT, 0);
//                }
//                mHandler.sendEmptyMessage(UPDATE_ADAPTER);
//            }
//        }
//    };
//
//    private List<GroupListAdapter.Item> createListData() {
//        items = new ArrayList<>();
//        GroupListAdapter.Item item;
//
//        //Silent
//        SilentButtonListener silentListener = new SilentButtonListener();
//        item = GroupListAdapter.createTagItem(getResources().getString(R.string.item_sound_silent_title), 40, 0);
//        items.add(item);
//
//        item = GroupListAdapter.createSlipItem(getResources().getString(R.string.item_sound_silent_mode), (mAudioMode != AudioManager.RINGER_MODE_NORMAL) ? R.drawable.ip_silent_on : R.drawable.ip_silent_off, mAudioMode != AudioManager.RINGER_MODE_NORMAL, silentListener);
//        items.add(item);
//
//        //Vibrate
//        item = GroupListAdapter.createTagItem(getResources().getString(R.string.setting_vibrate), 40, 0);
//        items.add(item);
//
//        //Vibrate on Ring
//        VibrateButtonListener mCheckListener = new VibrateButtonListener();
//        boolean vibrateWhenRing = Settings.System.getInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, 0) != 0;
//        GroupListAdapter.SlipListItem sliplistItem = GroupListAdapter.createSlipItem(getResources().getString(R.string.setting_vibrate_on_ring), 0, vibrateWhenRing, mCheckListener);
//        items.add(sliplistItem);
//
//        //Vibrate on Silent
//        SilentVibrateButtonListener mCheckListener1 = new SilentVibrateButtonListener();
//        GroupListAdapter.SlipListItem sliplistItem1 = GroupListAdapter.createSlipItem(getResources().getString(R.string.setting_vibrate_on_silent), 0, (Settings.System.getInt(getActivity().getContentResolver(), Settings.System.VIBRATE_IN_SILENT, 0) == 1), mCheckListener1);
//        items.add(sliplistItem1);
//
//        item = GroupListAdapter.createTagItem(getResources().getString(R.string.ring_reminder), 40, 0);
//        items.add(item);
//
//        //RingTone Volume
//        volumeSeekbarChangeListener listener = new volumeSeekbarChangeListener(getActivity());
//        GroupListAdapter.SeekBarListItem seekbar = GroupListAdapter.createSeekBarItem(GroupListAdapter.SeekBarListItem.SEEKBAR_TYPE_SOUND, 0, mVolumeMax, mVolumeCurrent, listener);//wangchong test
//        items.add(seekbar);
//
//        GroupListAdapter.SlipListItem sliplistItem2 = GroupListAdapter.createSlipItem(getResources().getString(R.string.use_key), 0, false, null);
//        items.add(sliplistItem2);
//
//        item = GroupListAdapter.createTagItem(getResources().getString(R.string.sound_detail), 40, 0);
//        items.add(item);
//
//        //RingTone
//        Intent intent = new Intent(getActivity(), RingToneListActivity.class);
//        GroupListAdapter.GroupListItem groupItem2 = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.incoming), intent);
//
//        Uri actualUri1 = RingtoneManager.getActualDefaultRingtoneUri(getActivity(), RingtoneManager.TYPE_RINGTONE);
//        if (actualUri1 != null) {
//            mRingtone = RingtoneManager.getRingtone(getActivity(), actualUri1);
//            if (mRingtone != null) {
//                groupItem2.mHint = mRingtone.getTitle(getActivity());
//                intent.putExtra("extra_tag", groupItem2.mHint);
//            }
//        }
//        items.add(groupItem2);
//
//        //NotificationTone
//        Intent intent2 = new Intent(getActivity(), NotificationToneListActivity.class);
//        GroupListAdapter.GroupListItem groupItem3 = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.setting_text_tone), intent2);
//        actualUri1 = RingtoneManager.getActualDefaultRingtoneUri(getActivity(), RingtoneManager.TYPE_NOTIFICATION); //RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//        //Log.d(TAG, "TYPE_NOTIFICATION actualUri1 :" +actualUri1);
//        if (actualUri1 != null) {
//            Ringtone mDefaultNotification = RingtoneManager.getRingtone(getActivity(), actualUri1);
//            if (mDefaultNotification != null) {
//                String actualTitle1 = mDefaultNotification.getTitle(getActivity());
//									/*int index = actualTitle1.indexOf('(');
//                                    int index1 = actualTitle1.indexOf(')');
//                                    if((index != -1)  && ( index1!=-1)){
//                                 		actualTitle1 = actualTitle1.substring(index+1,index1);
//                                    }*/
//                groupItem3.mHint = actualTitle1;
//                intent2.putExtra("extra_tag1", groupItem3.mHint);
//            }
//        } else {
//            groupItem3.mHint = getResources().getString(R.string.silent);
//            intent2.putExtra("extra_tag1", groupItem3.mHint);
//        }
//        items.add(groupItem3);
//
//
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.new_voicemail), null);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.new_mail), null);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.send_mail), null);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.setting_tweet), null);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.setting_facebook_post), null);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.calendar_alerts), null);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.ITEM_TYPE_TEXT_ICON, getResources().getString(R.string.reminder_alerts), null);
//        items.add(item);
//
//        item = GroupListAdapter.createItem(GroupListAdapter.TAG_TYPE_NORMAL);
//        items.add(item);
//
//        boolean lockScreenTone = Settings.System.getInt(mContentResolver, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0;
//        LockToggleButtonListener mCheckListener2 = new LockToggleButtonListener();
//        GroupListAdapter.SlipListItem sliplistItem11 = GroupListAdapter.createSlipItem(getResources().getString(R.string.lock_screen_tone), 0, lockScreenTone, mCheckListener2);
//        items.add(sliplistItem11);
//
//        // DTMF tone
//        boolean dTMFToneEnabled = Settings.System.getInt(mContentResolver, Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0;
//        DtmfoggleButtonListener mCheckListener3 = new DtmfoggleButtonListener();
//        GroupListAdapter.SlipListItem sliplistItem12 = GroupListAdapter.createSlipItem(getResources().getString(R.string.virtual_keypad_tone), 0, dTMFToneEnabled, mCheckListener3);
//        items.add(sliplistItem12);
//
//        item = GroupListAdapter.createItem(GroupListAdapter.TAG_TYPE_NORMAL);
//        items.add(item);
//        item = GroupListAdapter.createItem(GroupListAdapter.TAG_TYPE_SMALL);
//        items.add(item);
//        return items;
//    }
//
//    private class SilentButtonListener implements SlipButton.OnCheckedChangeListener {
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            boolean isVibrateMode = Settings.System.getInt(mContentResolver, Settings.System.VIBRATE_IN_SILENT, 0) == 1;
//            if (isChecked) {
//                if (isVibrateMode)
//                    mAudioMode = AudioManager.RINGER_MODE_VIBRATE;
//                else
//                    mAudioMode = AudioManager.RINGER_MODE_SILENT;
//            } else
//                mAudioMode = AudioManager.RINGER_MODE_NORMAL;
//
//            Log.d(TAG, "[onCheckedChanged] mAudioMode :" + mAudioMode);
//
//            if (mAudioManager != null)
//                mAudioManager.setRingerMode(mAudioMode);
//        }
//    }
//
//
//    private class VibrateButtonListener implements SlipButton.OnCheckedChangeListener {
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, isChecked ? 1 : 0);
//        }
//    }
//
//    private class SilentVibrateButtonListener implements SlipButton.OnCheckedChangeListener {
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_IN_SILENT, isChecked ? 1 : 0);
//
//            if (isChecked) {
//                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
//            } else {
//
//                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//            }
//
//        }
//    }
//
//    private class LockToggleButtonListener implements SlipButton.OnCheckedChangeListener {
//
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            Settings.System.putInt(mContentResolver, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, isChecked ? 1 : 0);
//        }
//    }
//
//    private class DtmfoggleButtonListener implements SlipButton.OnCheckedChangeListener {
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            Settings.System.putInt(mContentResolver, Settings.System.DTMF_TONE_WHEN_DIALING, isChecked ? 1 : 0);
//        }
//    }
//
//    // seekBar
//    private class volumeSeekbarChangeListener implements SeekBar.OnSeekBarChangeListener {
//        private int mLastProgress = -1;
//
//        public volumeSeekbarChangeListener(Context context) {
//            mLastProgress = mVolumeCurrent;
//        }
//
//
//        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
//            mLastProgress = progress;
//            if (mLastProgress > mVolumeMax)
//                mLastProgress = mVolumeMax;
//            if (!fromTouch) {
//                return;
//            }
//            saveVolume();
//        }
//
//        public void saveVolume() {
//            mAudioManager.setStreamVolume(AudioManager.STREAM_RING, mLastProgress, 0 /*flag*/);
//        }
//
//        public void onStartTrackingTouch(SeekBar seekBar) {
//            sample();
//        }
//
//        public void onStopTrackingTouch(SeekBar seekBar) {
//            stopSample();
//            sample();
//        }
//
//        private void sample() {
//            if (mRingtone != null && !mRingtone.isPlaying()) {
//                mRingtone.play();
//            }
//        }
//
//        public void stopSample() {
//            if (mRingtone != null) {
//                mRingtone.stop();
//            }
//        }
//    }
//
//    @Override
//    protected void onListItemClick(AdapterView<?> parent, View v, int position, long id) {
//        GroupListAdapter.Item item = (GroupListAdapter.Item) mAdapter.getItem(position);
//        if (item instanceof GroupListAdapter.GroupListItem) {
//            Intent intent = ((GroupListAdapter.GroupListItem) item).mIntent;
//            if (intent != null) {
//                startActivity(intent);
//            }
//        }
//    }
}
